//
//  HMEmoticonViewCell.swift
//  Weibo
//
//  Created by itteacher on 2017/8/1.
//  Copyright © 2017年 itteacher. All rights reserved.
//

import UIKit
//  自定义表情视图cell
class HMEmoticonViewCell: UICollectionViewCell {
    
    //  cell对应的数据源
    var emoticonArray: [HMEmoticon]? {
        didSet {
            
            
            guard let currentEmoticonArray = emoticonArray else {
                return
            }
            
            //  默认隐藏所有表情按钮
            for button in emoticonButtonArray {
                button.isHidden = true
            }
            
            //  遍历表情模型数组
            for (i, emoticon) in currentEmoticonArray.enumerated() {
                //  获取表情按钮
                let button = emoticonButtonArray[i]
                //  设置表情按钮对应的表情模型
                button.emoticon = emoticon
//                //  给按钮绑定数据显示按钮
//                button.isHidden = false
//                if emoticon.type == "0" {
//                    //  图片表情
//                    //  不能直接加载图片，因为图片在EmoitconBundle里面
////                    let image = UIImage(named: emoticon.png!)
////                    let path = HMEmoticonTools.sharedTools.emoticonBundle.resourcePath
////                    print(path)
//                    //  在指定bundle里面加载图片
//                    let image = UIImage(named: emoticon.path!, in: HMEmoticonTools.sharedTools.emoticonBundle, compatibleWith: nil)
//                    button.setImage(image, for: .normal)
//                    button.setTitle(nil, for: .normal)
//                } else {
//                    //  通过oc分类转成emoji表情
//                    let emoji = (emoticon.code! as NSString).emoji()
//                    button.setTitle(emoji, for: .normal)
//                    button.setImage(nil, for: .normal)
//                }
                
            }
            
        }
    }
    //  记录20个表情按钮
    private lazy var emoticonButtonArray: [HMEmoticonButton] = [HMEmoticonButton]()
    
    //  MARK: --    懒加载
    private lazy var messageLabel: UILabel = {
        let label = UILabel(fontSize: 20, textColor: UIColor.blue)
        return label
    }()
    
    //  删除表情按钮
    fileprivate lazy var deleteButton: UIButton = {
        let button = UIButton()
        //  添加点击事件
        button.addTarget(self, action: #selector(deleteButtonAction), for: .touchUpInside)
        button.setImage(UIImage(named: "compose_emotion_delete"), for: .normal)
        return button
    }()
    
    var indexPath: IndexPath? {
        didSet {
            guard let currentIndexPath = indexPath else {
                return
            }
            
            messageLabel.text = "当前滚动的是第\(currentIndexPath.section + 1)组第\(currentIndexPath.item + 1)页"
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        addChildButton()
        addSubview(messageLabel)
        messageLabel.snp.makeConstraints { (make) in
            make.center.equalTo(self)
        }
    }
    
    //  添加20个表情按钮
    private func addChildButton() {
        //  每项的宽度
        let buttonWidth = width / 7
        //  每项的高度
        let buttonHeight = height / 3
        for i in 0..<20 {
            //  计算列索引
            let colIndex = i % 7
            //  计算行的索引
            let rowIndex = i / 7
            
            let button = HMEmoticonButton()
            //  添加点击事件
            button.addTarget(self, action: #selector(emoticonButtonAction(btn:)), for: .touchUpInside)
            //  设置x，y
            button.x = CGFloat(colIndex) * buttonWidth
            button.y = CGFloat(rowIndex) * buttonHeight
            button.size = CGSize(width: buttonWidth, height: buttonHeight)
            //  设置文字大小
            button.titleLabel?.font = UIFont.systemFont(ofSize: 33)
            contentView.addSubview(button)
            //  记录表情按钮
            emoticonButtonArray.append(button)
        }
        
        contentView.addSubview(deleteButton)
        deleteButton.size = CGSize(width: buttonWidth, height: buttonHeight)
        deleteButton.x = width - buttonWidth
        deleteButton.y = height - buttonHeight
    }
    
    //  MARK: --    点击表情按钮的事件
    @objc private func emoticonButtonAction(btn: HMEmoticonButton) {
        //  获取表情按钮的模型
        let emoticon = btn.emoticon!
        //  发送通知
        NotificationCenter.default.post(name: NSNotification.Name(ClickEmoticonButtonNotificationName), object: emoticon)
    }
    
    //  MARK: --    点击删除表情按钮的事件
    @objc private func deleteButtonAction() {
        //  发送通知
        NotificationCenter.default.post(name: NSNotification.Name(ClickDeleteEmoticonButtonNotificationName), object: nil)
    }
    
}
